﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.DatabaseAccessLayer;
using Capstone_project.BussnessLogicLayer;


namespace Capstone_project
{
    public partial class frmUpdateTask : Form
    {
        public frmUpdateTask()
        {
            InitializeComponent();
        }
        StaffBUS staff = new StaffBUS();
        ProjectBUS proj = new ProjectBUS();
        TaskGroupBUS taskgroup = new TaskGroupBUS();
        AssignStatusBUS AssignStatus = new AssignStatusBUS();
        PriorityBUS priority = new PriorityBUS();

        
        SqlConnection con=Common.con;
        DataTable dt=new DataTable();
        SqlDataAdapter adap;

        private void frmUpdateTask_Load(object sender, EventArgs e)
        {
            //string sql = "SELECT TASK.ID_TASK,TASK.NAME_TASK,ASSIGN_TASK_STATUS.STATUS,TASK.COMPLETE,TASK_GROUP.NAME_TASK_GROUP,PRIORITY.LEVEL,PROJECT.NAME_PROJECT FROM ((ASSIGN_TASK_STATUS INNER JOIN TASK ON ASSIGN_TASK_STATUS.ID_TASK_STATUS = TASK.ID_TASK_STATUS) INNER JOIN TASK_GROUP ON TASK.ID_TASK_GROUP = TASK_GROUP.ID_TASK_GROUP) INNER JOIN PRIORITY ON TASK.ID_PRIORITY = PRIORITY.ID_PRIORITY WHERE TASK.ID_TASK='" + frmTaskManagement.selectedID + "'";
            //dt = Common.ReadDataTable(sql);

            cbxProject.DataSource = proj.getList();
            cbxProject.ValueMember = "ID_PROJECT";
            cbxProject.DisplayMember = "NAME_PROJECT";


            cbxTaskGroup.DataSource = taskgroup.getList();
            cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
            cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";


            cbxPriority.DataSource = priority.getList();
            cbxPriority.ValueMember = "ID_PRIORITY";
            cbxPriority.DisplayMember = "LEVEL";


            cbxStatus.DataSource = AssignStatus.getList();
            cbxStatus.ValueMember = "ID_TASK_STATUS";
            cbxStatus.DisplayMember = "STATUS";

            cbxStaff.DataSource = staff.getUserlist();
            cbxStaff.ValueMember = "USER_NAME";
            cbxStaff.DisplayMember = "USER_NAME";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string TASKID = txtTaskID.Text;
            string COMPLETE = txtComplete.Text;
            string IDPROJECT = cbxProject.SelectedValue.ToString();
            string IDTASKGROUP = cbxTaskGroup.SelectedValue.ToString();
            string IDPRIORITY = cbxPriority.SelectedValue.ToString();
            string IDSTATUS = cbxStatus.SelectedValue.ToString();
            string NAMEOFTASK = txtName.Text;
            string STARTDATE = dateTimePickerStartDate.Text;
            string ENDDATE = dateTimePickerEndDate.Text;
            string STAFF = cbxStaff.Text;

            string sql1 = "UPDATE TASK SET NAME_TASK='" + NAMEOFTASK + "',ID_TASK_STATUS='" + IDSTATUS + "',COMPLETE='" + COMPLETE + "', ID_PRIORITY='" + IDPRIORITY + "' WHERE ID_TASK='" + TASKID + "'";
            con.Open();
            //string sql2 = "update USER_TASK SET USER_NAME='"+STAFF+"',START_DATE='"+STARTDATE+"',END_DATE='"+ENDDATE+"' WHERE ID_TASK='"+TASKID+"'";
            //string sql3 = "UPDATE TASK_GROUP SET ID_PROJECT='"+IDPROJECT+"' WHERE ID_TASK_GROUP='"+IDTASKGROUP+"'";
            SqlCommand com = new SqlCommand(sql1, con);
            int kq = com.ExecuteNonQuery();
            con.Close();
            try
            {
                if (kq != 0) MessageBox.Show("Edit success");
                this.Close();
                frmAddNewTask tm = new frmAddNewTask();
                tm.Refresh();
            }
            catch { MessageBox.Show("Fail to Edit"); }
        }

        //private void cbxProject_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    string IDPROJECT=cbxProject.SelectedValue.ToString();
        //    cbxTaskGroup.DataSource = taskgroup.getTaskGrBYPROJECT(IDPROJECT);
        //    cbxTaskGroup.ValueMember = "ID_TASK_GROUP";
        //    cbxTaskGroup.DisplayMember = "NAME_TASK_GROUP";
        //}
    }
}
